package top.wineye.spring.dependency.inject;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.wineye.spring.dependency.pojo.Biz;

import java.util.Set;

@Configuration // not necessary, make intellij idea happy
public class DelayedInject {

    @Autowired
    ObjectProvider<Biz> bizProvider;

    @Autowired
    ObjectFactory<Set<Biz>> bizObjectFactory;

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx =
                new AnnotationConfigApplicationContext(DelayedInject.class);

        DelayedInject bean = ctx.getBean(DelayedInject.class);

        // not unique
        System.out.println(bean.bizProvider.getIfUnique(Biz::new));

        // 打印所有
        bean.bizProvider.forEach(System.out::println);

        // ObjectFactory 的方式
        System.out.println(bean.bizObjectFactory.getObject());

        ctx.close();
    }

    @Bean
    public Biz biz() {
        Biz biz = new Biz();
        biz.setBiz("1");
        return biz;
    }

    @Bean
    public Biz biz2() {
        Biz biz = new Biz();
        biz.setBiz("2");
        return biz;
    }
}
